package com.example.user.viewmodel;

import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.FileCtrlCom;
import com.example.common.component.UserCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.UserApiModifyInformationResp;
import com.example.common.user.GlobalUser;
import com.example.user.model.UserDetail;

public class UserModifyInfoViewModel extends ViewModel {
    private static final String TAG = "TAG";

    private FileCtrlCom fileCtrlCom;
    private UserCtrlCom userCtrlCom;

    private MutableLiveData<Boolean> isLoad = new MutableLiveData<>();
    private MutableLiveData<String> remainMsg=new MutableLiveData<>();//提示消息
    private MutableLiveData<Boolean> isUpDetail=new MutableLiveData<>();

    private UserDetail userDetail;//用户信息


    public UserModifyInfoViewModel(){
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData(){
        userDetail=new UserDetail();
        userCtrlCom=new UserCtrlCom();
        fileCtrlCom=new FileCtrlCom();
    }

    /**
     * 处理穿进来的bundle
     * @param bundle
     */
    public void dealBundle(Bundle bundle){
        userDetail.getUserName().setValue(bundle.getString("USERNAME"));
        userDetail.getIntroduction().setValue(bundle.getString("INTRODUCTION"));
        userDetail.getGender().setValue(bundle.getInt("GENDER"));
        userDetail.getBirthday().setValue(bundle.getString("BIRTHDAY"));
        userDetail.getAvatarUrl().setValue(bundle.getString("AVATAR"));
        isLoad.setValue(true);
    }

    /**
     * 上传头像
     * @param path 文件地址
     */
    public void upLoadAvatar(String path){
        String token= GlobalUser.TOKEN;
        fileCtrlCom.upLoadImg(path,token, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                Log.i(TAG, "onSuccess: "+response);
                userDetail.getAvatarUrl().setValue((String)response);
            }

            @Override
            public void onFailed(String mes) {
                Log.i(TAG, "onFailed: "+mes);
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 上传用户修改资料
     */
    public void upLoadDetailChange(){
        isUpDetail.setValue(false);
        userCtrlCom.putUser(GlobalUser.TOKEN,
                userDetail.getAvatarUrl().getValue(),
                userDetail.getBirthday().getValue(),
                userDetail.getGender().getValue(),
                userDetail.getIntroduction().getValue(),
                userDetail.getUserName().getValue(), new ICallback() {
                    @Override
                    public void onSuccess(Object response) {
                        remainMsg.setValue("修改成功！");

                        GlobalUser.USERNAME=userDetail.getUserName().getValue();
                        GlobalUser.AVATAR_URL=userDetail.getAvatarUrl().getValue();
                        GlobalUser.getInstance().getBindAvatarUrl().setValue(GlobalUser.AVATAR_URL);
                        GlobalUser.getInstance().getBindUserName().setValue(GlobalUser.USERNAME);
                        GlobalUser.getInstance().getBindIntroduction().setValue(userDetail.getIntroduction().getValue());

                        isUpDetail.setValue(true);
                    }

                    @Override
                    public void onFailed(String mes) {
                        remainMsg.setValue(mes);
                    }
                });
    }

    public int getGender(){
        if(userDetail.getGender().getValue()!=null){
            return userDetail.getGender().getValue();
        }else {
            return 0;
        }
    }

    public UserDetail getUserDetail() {
        return userDetail;
    }

    public MutableLiveData<Boolean> getIsLoad() {
        return isLoad;
    }

    public void setGender(int id){
        userDetail.getGender().setValue(id);
    }

    public void setBirthday(String birthday){
        userDetail.getBirthday().setValue(birthday);
    }

    public MutableLiveData<Boolean> getIsUpDetail() {
        return isUpDetail;
    }

    public MutableLiveData<String> getRemainMsg() {
        return remainMsg;
    }
}
